<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.currentId ? '新增分类' : '修改分类'"
    :close-on-click-modal="false"
    width="500px"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="100px"
      @onclick="onSubmit()"
    >
      <el-form-item
        label="分类名称"
        prop="name"
      >
        <el-input
          v-model="dataForm.name"
          placeholder="请输入分类名称"
        />
      </el-form-item>
      <el-form-item
        label="排序"
        prop="sortOrder"
      >
        <el-input-number
          v-model="dataForm.sortOrder"
          controls-position="right"
          :min="0"
          :max="9999"
        />
      </el-form-item>
      <el-form-item
        label="状态"
        prop="status"
      >
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">
            禁用
          </el-radio>
          <el-radio :label="1">
            启用
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="onSubmit()"
        >
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { Debounce } from '@/utils/debounce'

const emit = defineEmits(['refreshDataList'])
const visible = ref(false)
const dataForm = reactive({
  id: 0,
  currentId: 0,
  name: '',
  sortOrder: 0,
  status: 1
})

const dataRule = reactive({
  name: [
    { required: true, message: '分类名称不能为空', trigger: 'blur' },
    { min: 2, max: 50, message: '分类名称长度在2-50之间', trigger: 'blur' }
  ]
})

const isSubmit = ref(false)
const dataFormRef = ref(null)

const init = (id) => {
  dataForm.currentId = id || 0
  dataForm.id = id || 0
  nextTick(() => {
    dataFormRef.value?.resetFields()
  })
  if (dataForm.id) {
    // 修改
    http({
      url: http.adornUrl(`/admin/courseCategory/info/${dataForm.id}`),
      method: 'get',
      params: http.adornParams()
    })
      .then(({ data }) => {
        visible.value = true
        dataForm.id = data.id
        dataForm.name = data.name
        dataForm.sortOrder = data.sortOrder
        dataForm.status = data.status
      })
  } else {
    visible.value = true
  }
}

defineExpose({ init })

// 表单提交
const onSubmit = Debounce(() => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      if (isSubmit.value) {
        return
      }
      isSubmit.value = true
      http({
        url: http.adornUrl('/admin/courseCategory/' + (dataForm.id ? 'update' : 'add')),
        method: 'post',
        data: http.adornData({
          id: dataForm.id || undefined,
          name: dataForm.name,
          sortOrder: dataForm.sortOrder,
          status: dataForm.status
        })
      })
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 1000,
            onClose: () => {
              isSubmit.value = false
              visible.value = false
              emit('refreshDataList')
            }
          })
        })
        .catch(() => {
          isSubmit.value = false
        })
    }
  })
})
</script>

<style scoped>
</style>

